package com.pigotech.ptwo.UI.Activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.pigotech.ptwo.R;
import com.pigotech.ptwo.connect.IChannelListener;
import com.pigotech.ptwo.UI.CustomView.CustomToast;

/**
 * Created by ghb on 2017/12/14.
 */

public class BaseActivity extends Activity implements IChannelListener {

    private boolean isExit = false;// 定义一个变量，来标识是否退出
    final int FLAG_HOMEKEY_DISPATCHED = 0x80000000;
    public int statusBarHeight = -1;//系统状态栏高度
    @Override
    public void onChannelEvent(int type, Object param, String... array) {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(FLAG_HOMEKEY_DISPATCHED, FLAG_HOMEKEY_DISPATCHED);//关键代码
        getStatusBarHeight();
    }

    /**
     * 获取系统状态栏高度
     */
    private void getStatusBarHeight() {
        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight = getResources().getDimensionPixelSize(resourceId);
        }
    }

    @Override
    protected void onPause() {
//        if (!VideoDownloadTaskManager.getInstance().isDownloading()){//如果视频不存在下载 断开wifi
//            UtilsLifeCycleManager utilsLifeCycleManager=UtilsLifeCycleManager.getInstance();
//            utilsLifeCycleManager.onPause();
//        }
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 重回软件 重新连接WIFI
//        UtilsLifeCycleManager utilsLifeCycleManager=UtilsLifeCycleManager.getInstance();
//        utilsLifeCycleManager.onResume();
    }

    @Override
    protected void onStop() {
//        if (!VideoDownloadTaskManager.getInstance().isDownloading()){//如果视频不存在下载 断开wifi
//            UtilsLifeCycleManager utilsLifeCycleManager=UtilsLifeCycleManager.getInstance();
//            utilsLifeCycleManager.onStop();
//        }
        super.onStop();
    }

    @Override
    public void onBackPressed() {
        if (!isExit) {
            isExit = true;
            CustomToast.show(getApplicationContext(), getResources().getString(R.string.isexit),
                    Toast.LENGTH_SHORT);
            // 利用handler延迟发送更改状态信息
            isExitHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
//            if (VideoDownloadTaskManager.getInstance().isDownloading()) {//正在下载弹出警告对话框
//                showWarningDialog();
//            } else {
//                super.onBackPressed();
//            }
        }
    }

    /**是否退出的handler */
    private Handler isExitHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };
}
